﻿using B.W.traceability.Domain.养殖;
using B.W.traceability.ErrorCode;
using B.W.traceability.Infrastructure;
using B.W.traceability.Read.API.Application.APPlicationCommand;
using B.W.traceability.Read.API.DTO;
using MediatR;
using static System.Net.Mime.MediaTypeNames;

namespace B.W.traceability.Read.API.Application.APPlicationHanlder
{
    public class TraceabilityShowHandler : IRequestHandler<TraceabilityShowCommand, APIResult<APIPResult<TraceabilityDTO>>>
    {
        private readonly IBInfrastructure<Traceabilitytable> TraceabilitytableAPIResult;
        private readonly IBInfrastructure<TraceabilityNames> TraceabilityNamesAPIResult;

        public TraceabilityShowHandler(IBInfrastructure<Traceabilitytable> traceabilitytableAPIResult, IBInfrastructure<TraceabilityNames> traceabilityNamesAPIResult)
        {
            TraceabilitytableAPIResult = traceabilitytableAPIResult;

        }

        public Task<APIResult<APIPResult<TraceabilityDTO>>> Handle(TraceabilityShowCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPResult<TraceabilityDTO>> result = new APIResult<APIPResult<TraceabilityDTO>>();
            result.Code = ErrorAPIResult.显示成功;
            result.Msg = ErrorAPIResult.显示成功.ToString();

            var list = from a in TraceabilitytableAPIResult.Query()
                       where !a.MyProperty
                       select new TraceabilityDTO
                       {
                           Traceabilityid = a.Traceabilityid,
                           TraceabilityNamesid = a.Traceabilityid,
                           TraceabilityDateTime = a.TraceabilityDateTime,
                           TraceabilityDate = a.TraceabilityDate,
                           Traceabilitypermit = a.Traceabilitypermit,
                           enterprise = a.enterprise,
                           Image = a.Image,

                       };
            int Count = list.Count();
            int PageCount = (int)Math.Ceiling(Count * 1.0 / request.PageSize);
            list = list.OrderByDescending(x => x.Traceabilityid).Skip(request.PageSize * (request.PageIndex - 1)).Take(request.PageSize);
            APIPResult<TraceabilityDTO> page = new APIPResult<TraceabilityDTO>();
            page.TotalCount = Count;
            page.TotalPage = PageCount;
            page.list = list.ToList();
            result.Data = page;
            return Task.FromResult(result);
        }
    }
}
